<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/3/11
 * Time: 22:05
 */

namespace Home\Controller;

use Think\Controller;

class StuformController extends Controller
{

    public function _initialize()
    {
        header("content-type:text/html; charset=utf-8");
        $flag = session("?stuid");
        if (!$flag) {
            $this->error("未登录", U('Login/index'));
        }
    }

    public function oneAdd()
    {
        $post["enid"] = I("enid");
        $post["stuid"] = session("stuid");
        $post["addtime"] = time();

        $stuenroll = M("stuenroll");
        if ($stuenroll->add($post)) {
            $this->success("报名成功", U("Index/index"));
        } else {
            $this->error("报名失败");
        }
    }

    public function team()
    {
        if (IS_POST) {
            $post = I("post.");

            $teamData = array(
                "enid" => $post["enid"],
                "grade" => "",
                "addtime" => time()
            );
            $teaminfo = D("TeamInfo");
            $team = D("Team");

            if ($teamid = $team->add($teamData)) {
                if (!empty($post["stuid"])) {
                    $data[] = array(
                        "teamid" => $teamid,
                        "stuid" => $post["stuid"],
                        "captain" => 1
                    );
                }

                for ($i = 1; $i < 5; $i++) {
                    if (!empty($post["name$i"])) {
                        $data[] = array(
                            "teamid" => $teamid,
                            "stuid" => $post["stuid$i"],
                            "captain" => 0
                        );
                    }
                }
                if ($teaminfo->addAll($data)) {
                    $this->success("报名成功", U("Index/index"));
                } else {
                    $this->error("报名失败");
                }
            }
        } else {
            $con["enid"] = I("get.enid");
            $EnInfo = D("EnInfo");
            $ret = $EnInfo->where($con)->find();
            $ename = $ret["title"];
            $depData = D("DeptInfo")->select();
            $this->assign("ename", $ename)->assign("enid", $con["enid"])->assign("depData", $depData);
            $this->display();
        }
    }

    public function teamAdd()
    {
        $post = I("post.");
//        dump($post);
        if (!empty($post["stuid"])) {
            $data[] = array(
                "stuid" => $post["stuid"],
                "captain" => 1
            );
        }
        for ($i = 1; $i < 5; $i++) {
            if (!empty($post["name$i"])) {
                $data[] = array(
                    "stuid" => $post["stuid$i"],
                    "captain" => 0
                );
            }
        }

        if (count($data) < 3) {
            $this->error("最少要3人！");
        }
        $teamData = array(
            "enid" => $post["enid"],
            "grade" => "",
            "addtime" => time()
        );
        $teaminfo = D("TeamInfo");
        $team = D("Team");
        if ($teaminfo->addAll($data) && $team->add($teamData)) {
            $this->success("报名成功", U("Index/index"));
        } else {
            $this->error("报名失败");
        }

    }

    public function everyOne()
    {

        $input = I("get.term");
        $StuInfo = D("StuInfo");
        $where["name"] = array('like', '%' . $input . '%');
        $ret = $StuInfo->where($where)->select();
        if (count($ret) > 0) {
            foreach ($ret as $vo) {
                $ClassInfo = D("ClassInfo");
                $Class = $ClassInfo->getItem($vo["classid"]);
                $result[] = array(
                    'label' => $Class["name"] . " / " . $vo['name'],
                    'value' => $vo['stuid']
                );
            }
            echo json_encode($result);
        } else {
            $result[] = array(
                'label' => "没有这位同学",
                'value' => ""
            );
            echo json_encode($result);
        }
    }

    public function checkStu()
    {
        $stuid = I("post.stuid");
        $name = I("post.name");
        $StuInfo = D("StuInfo");
        $ret = $StuInfo->getItem($stuid);
        if ($name == $ret["name"]) {
            $this->ajaxReturn(true);
        } else {
            $this->ajaxReturn(false);
        }
    }

}